%%

close all;
clear;
I=imread('4.jpg');
G=rgb2gray(I);
C=corner(G);

%% Plot the picture
figure(1); imshow(I); hold on; plot(C(:,1),C(:,2),'rx');

%%


%{find lowest point, accurate%}
y_down=max(C(:,2));
for i=1:length(C )
if (C(i,2)==y_down)
x_down = C(i,1);
end
end
%{find leftest point, inaccurate%}
x_left=min(C(:,1));
for i =1:length(C )
if (C(i,1)==x_left)
y_left=C(i,2);
end
end
%{calculate the distance%}
x_distance=x_down-x_left

%{find left point, inaccurate%}
y_up = y_down-4/5*x_distance
for i=1:length(C )
if (C(i,2)==y_up)
x_up = C(i,1);
end
end

%{find highest point, accurate%}
y_max_up = y_down
for i=1:length(C )
	if (C(i,2) < y_max_up)
		if (C(i,2) > y_up)
			y_max_up = C(i,2);
			x_max_up = C(i,1);
		end
	end
end

%{find rightest point, accurate%}
x_right=max(C(:,1));
for i=1:length(C )
if (C(i,1)==x_right)
y_right=C(i,2);
end
end

%{find most left point, accurate%}
x_max_left = x_right
for i=1:length(C )
if (C(i,1) < x_max_left)
	if(C(i,1) > x_left)
		if(C(i,2) < y_down)
			if (C(i,2) > y_max_up)
				x_max_left = C(i,1);
				y_max_left = C(i,2);
			end
		end
	end
end
end

%%
figure(2);
I2=imcrop(I,[x_max_left,y_max_up,(x_right-x_max_left),(y_down-y_max_up)]);
imshow(I2)
hold on 
plot(x_max_left, y_max_left,'r*')
plot(x_right, y_right, 'g*')
plot(x_down, y_down, 'b*')
plot(x_max_up, y_max_up, 'w*')
